﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSCONORD
    {
        MDOgeneral mod = new MDOgeneral();
        string nroord_cor, codmat_cor, codcol_cor;
        double cansta_cor, canent_cor, candev_cor;

        public string NROORD_COR
        {
            get { return nroord_cor; }
            set { nroord_cor = value; }
        }

        public string CODMAT_COR
        {
            get { return codmat_cor; }
            set { codmat_cor = value; }
        }

        public string CODCOL_COR
        {
            get { return codcol_cor; }
            set { codcol_cor = value; }
        }

        public double CANSTA_COR
        {
            get { return cansta_cor; }
            set { cansta_cor = value; }
        }

        public double CANENT_COR
        {
            get { return canent_cor; }
            set { canent_cor = value; }
        }

        public double CANDEV_COR
        {
            get { return candev_cor; }
            set { candev_cor = value; }
        }

        public CLSMAEMAT material = new CLSMAEMAT();


       public int insertar(){
            try{
                string query = "insert into CONORD (NROORD_COR,CODMAT_COR,CODCOL_COR,CANSTA_COR,CANENT_COR,CANDEV_COR) values ('"+NROORD_COR+"','"+CODMAT_COR+"','"+CODCOL_COR+"',"+CANSTA_COR+","+CANENT_COR+","+CANDEV_COR+")";
                return mod.comando(query);
           }catch(Exception ex){throw ex;}
        }

        public void Actualizar()
        {
            try
            {
                string SQL = "update conord set canent_cor = canent_cor + " + CANENT_COR + " where nroord_cor='" + NROORD_COR + "' and codmat_cor='" + CODMAT_COR + "' and codcol_cor='" + CODCOL_COR + "'";
                mod.comando(SQL);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void actCanDev()
        {
            try
            {
                string SQL = "update conord set CANDEV_COR = CANDEV_COR + " + CANDEV_COR + " where nroord_cor='" + NROORD_COR + "' and codmat_cor='" + CODMAT_COR + "' and codcol_cor='" + CODCOL_COR + "'";
                mod.comando(SQL);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Boolean existe() {
            try {
                string query = "select count(*) from CONORD where nroord_cor='" + NROORD_COR + "' and codmat_cor='" + CODMAT_COR + "' and codcol_cor='" + CODCOL_COR + "'";
                DataTable val=   mod.consulta(query);
                if (val.Rows.Count > 0) {
                    if (Convert.ToInt32(val.Rows[0][0])>0) {
                        return true;                    
                    }
                }
                return false;
            }
            catch (Exception ex) { throw ex; }
        }

        public double CantidadEntregada()
        {
            try
            {
                DataTable dt = new DataTable();
                string query = "select canent_cor from conord where nroord_cor='" + NROORD_COR + "' and codmat_cor='" + CODMAT_COR + "' and codcol_cor='" + CODCOL_COR + "'";
                dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    return System.Convert.ToDouble(dt.Rows[0][0].ToString());
                }
                return 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable consConsumoOrden() 
        {
            try {

                string query = "select a.nroord_cor, a.codmat_cor, b.nommat_mat, a.codcol_cor, c.nomcol_col, a.cansta_cor, a.canent_cor, a.candev_cor from conord a inner join maemat b on a.codmat_cor=b.codmat_mat inner join maecolp c on a.codcol_cor=c.codcol_col where a.nroord_cor='"+NROORD_COR+"'";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }


        public DataTable consOrdenReparacion()
        {
            try
            {

                string query = " select CODCLI_ERP, NOMCLI_CLI, NROREP_ERP, TIPREP_ERP, DOCDET_DRP, TIPDET_DRP from detrepp a inner join encrepp b on a.nrorep_drp=b.nrorep_erp  and a.codcli_drp=b.codcli_erp inner join maeclip c on b.codcli_erp = c.codcli_cli where b.nroord_erp = '" + NROORD_COR + "'";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public DataTable TallasOrden(string strNroOrden)
        {
            try
            {

                string query = "select distinct e.codtal_tal from encordp a inner join detordp b on a.nroord_eop = b.nroord_dop inner join maeref c on b.codref_dop=c.codref_ref inner join maecol d on b.codcol_dop=d.codcol_col inner join taltal e on c.talref_ref=e.codtje_tal where a.nroord_eop= '" + strNroOrden + "' order by codtal_tal ";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public DataTable conConOrdRef()
        {
            try
            {
                string query = "select nroord_cor,codref_eop,(select nomref_ref from maeref where codref_ref=codref_eop) as nomref_ref,codcol_eop,(select nomcol_col from maecol where codcol_col=codcol_eop) as nomcol_eop,(select sum(canord_dop) from detordp where nroord_dop=nroord_cor)  as canord_dop,codmat_cor,codcol_cor,nommat_mat,canent_cor,candev_cor,(select nomcol_col from maecolp where codcol_col=codcol_cor) as nomcol_mat from encordp,conord,maemat where (codmat_mat=codmat_cor and  nroord_eop=nroord_cor and codgrp_mat='C') and  nroord_cor='"+NROORD_COR+"'"; 
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }




    }
}
